package com.bdqn.design.factory;

import com.bdqn.reflect.dao.PersonDao;
import com.bdqn.reflect.dao.impl.PersonMySQLDao;
import com.bdqn.reflect.dao.impl.PersonOracleDao;
import com.bdqn.reflect.dao.impl.PersonRedisDao;
import com.bdqn.reflect.dao.impl.PersonSQLServerDao;

/**
 * 简单工厂模式
 *
 * @author LILIBO
 * @since 2025/1/6
 */
public class SimpleDaoFactory {

    /**
     * 获取PersonDao实例的简单工厂
     *
     * @return
     */
    public static PersonDao getInstance() {
        return new PersonRedisDao();
    }

    /**
     * 获取PersonDao实例的简单工厂
     *
     * @param key 数据库标记（可以提供一个实现类的全路径，通过反射创建实例）
     * @return
     */
    public static PersonDao getInstance(String key) {
        switch (key) {
            case "MySQL":
                return new PersonMySQLDao();
            case "Oracle":
                return new PersonOracleDao();
            case "SQLServer":
                return new PersonSQLServerDao();
            case "Redis":
                return new PersonRedisDao();
            default: {
                try {
                    Class clazz = Class.forName(key);
                    return (PersonDao) clazz.newInstance();
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
                    throw new RuntimeException("无效的数据库类型：" + key + "，DAO获取失败");
                }
            }
        }
    }

}
